﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Windows.Forms;
using NetJsWire.JsonRpcHandler;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Core;
using NetJsWire.Widgets.Renderers.Core;
using NetJsWire.Widgets.Ui.Layout;

namespace NetJsWire.Widgets.Renderers.Tabs
{
    [ ClassName( "Ext.TabPanel" ) ]
    internal class TabControl : Widget, IRenderer
    {
        public TabControl( object baseObject )
            : base( baseObject )
        {
        }

        public LayoutType Layout
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public bool AutoPositionedButtons
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #region IRenderer Members

        public override void Render()
        {
            if ( !Rendered )
            {
                if ( BaseParent is IWidgetContainer )
                {
                    SetAttribute( ControlNode, "isInContainer", true );
                    SetConstructionProperty( "isInContainer", true );
                }
                else
                {
                    SetStyleProperty( "position", "absolute" );
                    SetConstructionProperty( "renderTo", ClientParentToRender );
                }

                SetAttribute( "isContainer", true );

                EnsureParentNodePosition();
                //if (BaseParent is IWidget && !(BaseParent as IWidget).Renderer.Rendered)
                //    (BaseParent as IWidget).Renderer.Render();

                ControlNode.AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element", "pages", "" ) );
                SetConstructionProperty( "width", ( ( Control ) Base ).ClientSize.Width );
                SetConstructionProperty( "height", ( ( Control ) Base ).ClientSize.Height - 20 );
                SetConstructionProperty( "enableTabScroll", true );
                SetConstructionProperty( "layoutOnTabChange", true );
                SetConstructionProperty( "layoutOnTabChange", true );
                //SetConstructionProperty( "renderTo", ClientParentToRender );
                //SetStyleProperty( "position", "absolute" );

                //tabPosition:'bottom'
                if ( ( Base as Ui.Tabs.TabControl ).Alignment == TabAlignment.Bottom )
                {
                    SetConstructionProperty( "enableTabScroll", false );
                    SetConstructionProperty( "tabPosition", "bottom" );
                }

                foreach ( Ui.Tabs.TabPage tp in ( Base as Ui.Tabs.TabControl ).TabPages )
                {
                    AddTab( tp, false );
                }
                SetConstructionProperty( "activeTab", ( ( Ui.Tabs.TabControl ) Base ).SelectedIndex );
                //ControlNode.Attributes.Append(WebApplication.Instance.XmlDocument.CreateAttribute("selected")).Value = (Base as Ui.Tabs.TabControl).SelectedIndex.ToString();
                //base.Render();
            }

            base.Render();
        }

        #endregion

        internal void AddTab( Ui.Tabs.TabPage newTab, bool changeParent )
        {
            if ( changeParent )
            {
                newTab.Parent = null;
                ( ( Ui.Tabs.TabControl ) Base ).TabPages.Add( newTab );
                newTab.Parent = ( Base as Ui.Tabs.TabControl );
            }

            if ( ControlNode[ "pages" ] == null )
                ControlNode.AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element", "pages", "" ) );

            var node =
                ControlNode[ "pages" ].AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element", "page",
                                                                                                    "" ) );
            //XmlNode items = node.AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element", "items", "" ) );
            SetAttribute( node, "name", newTab.Name );
            SetAttribute( node, "text", newTab.Text );
            if ( !string.IsNullOrEmpty( newTab.ToolTipText ) )
            {
                SetAttribute( node, "toolTip", newTab.ToolTipText );
            }

            //foreach ( Control control in newTab.Controls )
            //{
            //    if ( control is IWidget )
            //    {
            //        IWidget ctrl = ( IWidget ) control;

            //        XmlNode item = items.AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element", "item", "" ) );

            //        XmlAttribute attr = WebApplication.Instance.XmlDocument.CreateAttribute( "value" );
            //        attr.Value = ctrl.Renderer.ClientID;
            //        item.Attributes.Append( attr );
            //    }
            //}

            //node.Attributes.Append(WebApplication.Instance.XmlDocument.CreateAttribute("name")).Value = newTab.Name;
            //node.Attributes.Append(WebApplication.Instance.XmlDocument.CreateAttribute("text")).Value = newTab.Text;
            //node.Attributes.Append(WebApplication.Instance.XmlDocument.CreateAttribute("toolTip")).Value = newTab.ToolTipText;

            if ( !( newTab is Ui.Tabs.TabPage ) ) return;

            if ( ( newTab ).Closable )
            {
                SetAttribute( node, "closable", true );
            }
            //node.Attributes.Append(WebApplication.Instance.XmlDocument.CreateAttribute("closable")).Value = "true";
            //if ((newTab as NetJsWire.Widgets.Ui.Tabs.TabPage).Layout != NetJsWire.Widgets.Ui.Layout.LayoutType.)
            SetAttribute( node, "layout", ( newTab ).Layout.ToString().ToLower() );
        }
    }
}